<template>
    <div>
        <el-row>
            <el-col :span="12">
                <el-form :inline="true" :model="formInline" class="demo-form-inline">
                <el-form-item label="业主姓名">
                    <el-input v-model="formInline.name" placeholder="业主姓名"></el-input>
                </el-form-item>
                <el-form-item label="公司名称">
                    <el-select v-model="formInline.region" placeholder="公司名称">
                    <el-option label="区域一" value="shanghai"></el-option>
                    <el-option label="区域二" value="beijing"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="search">查询</el-button>
                </el-form-item>
                </el-form>
            </el-col>
            <el-col :span="12" class="btn-box">
                <el-row>
                    <el-button type="success">增加</el-button>
                    <el-button type="warning">导入</el-button>
                    <el-button type="danger">停用</el-button>
                    <el-button>恢复</el-button>
                </el-row>
            </el-col>   
        </el-row>
        <informationTable 
        :columns="columns" 
        :dataSource="tableData" 
        :options="options"
        :fetch="fetchTableData"
        :pagination="pagination"
        @row-click="handleRowClick"
        />
       <el-dialog
        title="业主信息"
        :visible.sync="dialogInformation"
        width="30%"
        >
            <div class="dialog-body">
                <el-form ref="form" :model="form" label-width="80px">
                    <el-form-item label="公司名称">
                        <el-input v-model="form.company" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="业主编码">
                        <el-input v-model="form.code" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="业主姓名">
                        <el-input v-model="form.name"></el-input>
                    </el-form-item>
                    <el-form-item label="业主性别">
                        <el-input v-model="form.gender" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="出生日期">
                        <el-input v-model="form.birthDate"></el-input>
                    </el-form-item>
                    <el-form-item label="证件编码">
                        <el-input v-model="form.card"></el-input>
                    </el-form-item>
                    <el-form-item label="手机号码">
                        <el-input v-model="form.tel"></el-input>
                    </el-form-item>
                    <el-form-item label="状态">
                        <el-input v-model="form.state"></el-input>
                    </el-form-item>
                    <el-form-item label="手机号码">
                        <el-input v-model="form.tel"></el-input>
                    </el-form-item>
                    <el-form-item label="状态">
                        <el-input v-model="form.state"></el-input>
                    </el-form-item>
                    <el-form-item label="手机号码">
                        <el-input v-model="form.tel"></el-input>
                    </el-form-item>
                    <el-form-item label="状态">
                        <el-input v-model="form.state"></el-input>
                    </el-form-item>
                    <el-form-item label="手机号码">
                        <el-input v-model="form.tel"></el-input>
                    </el-form-item>
                    <el-form-item label="状态">
                        <el-input v-model="form.state"></el-input>
                    </el-form-item>
                </el-form>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogInformation = false">取 消</el-button>
                <el-button type="primary" @click="onSubmitDialog">确 定</el-button>
            </span>
        </el-dialog>
        <el-dialog
        title="家庭成员"
        :visible.sync="dialogFamily"
        width="30%"
        >
            <div class="dialog-body">
                <el-table
                    :data="familyData"
                    height="400"
                    border
                    style="width: 100%">
                    <el-table-column
                    prop="name"
                    label="姓名"
                    width="80">
                    </el-table-column>
                    <el-table-column
                    prop="gender"
                    label="性别"
                    width="60">
                    </el-table-column>
                    <el-table-column
                    prop="tel"
                    label="电话">
                    </el-table-column>
                    <el-table-column
                    prop="card"
                    label="证件">
                    </el-table-column>
                    <el-table-column
                    prop="work"
                    label="工作单位">
                    </el-table-column>
                    <el-table-column
                    prop="filiation"
                    label="关系">
                    </el-table-column>
                </el-table> 
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogFamily = false">取 消</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
import axios from 'axios'
import informationTable from '@/components/table'
export default {
    components: {
        informationTable
    },
    data(){
        return {
            formInline: {
                name: '',
                region: ''
            },
            dialogInformation: false,
            dialogFamily: false,
            form: {
                company: '',
                code: '',
                name: '',
                gender: '',
                birthDate: '',
                card: '',
                tel: '',
                state: '',
            },
            tableData:[],
            familyData:[],
            columns: [
                {
                    prop: 'company',
                    label: '所属公司',
                    fixed: 'left',
                    width: '200'
                },
                {
                    prop: 'code',
                    label: '业主编码',
                    width: '200'
                },
                {
                    prop: 'name',
                    label: '业主姓名',
                    width: '200'
                },
                {
                    prop: 'gender',
                    label: '业主姓别',
                    width: '200',
                    render: (row, index) => { 
                        return (
                            <span>{row.gender === 1 ? '男' : row.gender === 2 ? '女' : '未知'}</span>
                        )
                    }
                },
                {
                    prop: 'birthDate',
                    label: '出生日期',
                    width: '200'
                },
                {
                    prop: 'card',
                    label: '证件编码',
                    width: '200'
                },
                {
                    prop: 'tel',
                    label: '手机号码',
                    width: '200'
                },
                {
                    prop: 'state',
                    label: '状态',
                    width: '100',
                    render: (row, index) => { 
                        if(row.state === 1){
                            return (<span style="color:green">正常</span>)
                        }else{
                            return (<span style="color:red">欠费</span>)
                        }       
                    }
                },
                {
                    prop: 'family',
                    label: '家庭成员',
                    fixed: 'right',
                    width: '200',
                    render: (row, index) => { 
                         return (<span style="color:blue" onClick={e => this.familyClick(e, row)}>家庭成员({row.family})</span>)     
                    }
                },
                {
                    button: true,
                    label: '按钮组',
                    fixed: 'right',
                    width: '200',
                    group: [
                        {
                            name: '编辑',
                            type: 'warning',
                            icon: 'el-icon-edit',
                            plain: true,
                            onClick: (row, index) => { // 箭头函数写法的 this 代表 Vue 实例 
                                this.dialogInformation = true
                            }
                        },
                        {
                            name: '删除',
                            type: 'danger',
                            icon: 'el-icon-delete',
                            plain: true,
                            onClick: (row, index) => { // 箭头函数写法的 this 代表 Vue 实例 
                                this.$confirm('是否进行删除操作?', '提示', {
                                    confirmButtonText: '确定',
                                    cancelButtonText: '取消',
                                    type: 'warning'
                                }).then(() => {
                                    this.$message({
                                        message: '对不起,您没有权限进行该操作!',
                                        type: 'error'
                                    });
                                    this.dialogInformation = false
                                }).catch(() => {
                                    this.$message({
                                        type: 'info',
                                        message: '已取消'
                                    });          
                                }); 
                            }
                        }
                    ]
                }
            ],
            pagination: {
                total: 0,
                pageIndex: 1,
                pageSize: 10
            },
            options: {
                mutiSelect: false,
                index: false, // 显示序号， 多选则 mutiSelect
                loading: false, // 表格动画
                initTable: true, // 是否一挂载就加载数据
            }
        }
    },
    methods: {
        handleRowClick: function(e, row){
           // alert("3");
        },
        fetchTableData: function(){
            this.options.loading = true
            axios.post('https://www.easy-mock.com/mock/5cb04e65601f9652ee871c6b/example/informationTable', {
                page: this.pagination.pageIndex,
                pageSize: this.pagination.pageSize
            }).then(res => {
                const { list, total } = res.data.data
                this.tableData = list
                this.pagination.total = total
                this.options.loading = false
            }).catch((error) => {
                console.log(error)
                this.options.loading = false
            })
        },
        search() {
            this.fetchTableData();
        },
        onSubmitDialog(){
            this.$confirm('是否保存编辑操作?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$message({
                    message: '恭喜你，操作成功!',
                    type: 'success'
                });
                this.dialogInformation = false
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });          
            }); 
        },
        familyClick: function(e,row){
           //  console.log(row);
           axios.post('https://www.easy-mock.com/mock/5cb04e65601f9652ee871c6b/example/familyData', {
               
            }).then(res => {
               this.familyData = res.data.data;
            }).catch((error) => {
               console.log(error)
            })
           this.dialogFamily = true
        }
    }
}
</script>
<style scoped>
    .dialog-body{
        height: 400px !important;
        overflow: auto !important;
    }
    .btn-box{
        text-align: right
    }
</style>
